﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
public partial class Manage_HospitalLook : System.Web.UI.Page
{
    public string strConditions;
    public string _AreaId = "", Id_AreaId;
    protected void Page_Load(object sender, EventArgs e)
    {

        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        _AreaId = SysFun.ToTrim(_Account.AreaId);
        Id_AreaId = SysFun.ToTrim(_Account.AreaId);
        int i = 0;

        if (!IsPostBack)
        {
            int _intAreaid = SysFun.ToInt(_AreaId), _intAreaIdMax = 0;
            string strWhere = "";
            if (_intAreaid != 530000)
            {
                if (_intAreaid % 100 > 0)
                {
                    strWhere += "   Hospital.areaid=" + _intAreaid.ToString();
                }
                else
                {
                    _intAreaIdMax = _intAreaid + 100;
                    strWhere += " Hospital.areaid>=" + _intAreaid.ToString() + " And Hospital.AreaId< " + _intAreaIdMax.ToString();
                }
            }
            else
            {
                strWhere += " Hospital.areaid>530000 And Hospital.AreaId< 419900 ";
            }
            Db DbProject = new Db();
            string strSql = "select distinct area.areaname,area.areaid from area,hospital where area.areaid=Hospital.areaid and " + strWhere;
            System.Data.IDataReader dr = DbProject.DataReader(strSql);
            lstAreaId.Items.Add("全部");
            while (dr.Read())
            {

                lstAreaId.Items.Add(dr["areaname"].ToString());
                lstAreaId.Items[++i].Value = dr["areaid"].ToString();

            }
            lstAreaId.DataBind();
            dr.Close();
            dr.Dispose();
            DbProject.Close();
            lstDistinction.Items.Add("全部");
            lstDistinction.Items.Add("三甲");
            lstDistinction.Items.Add("三乙");
            lstDistinction.Items.Add("二甲");
            lstDistinction.Items.Add("二乙");
            lstDistinction.Items.Add("一甲");
            lstDistinction.Items.Add("一乙");
        }

        showGridView();

    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "hospital left join area on hospital.areaid=area.areaid";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "area.areaname,accountid,HospitalName,UserCode, case distinction when '0' then '三甲' when '1' then '三乙' when '2' then '二甲' when '3' then '二乙' when '4' then '一甲' when '5' then '一乙'end as distinction, case sort when '0' then '综合' when '1' then '专科' when '2' then '中医' end as sort";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 24;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HospitalName,UserCode";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        dtlHospital.DataSource = dr;
        dtlHospital.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", HospitalName = "", _UserCode, _AreaId, _Distinction;
        HospitalName = txtHospitalName.Text.ToString();
        _UserCode = txtUserCode.Text.ToString();
        _AreaId = lstAreaId.SelectedValue;
        _Distinction = lstDistinction.Text;
        switch (_Distinction)
        {
            case "全部":
                _Distinction = "全部";
                break;
            case "三甲":
                _Distinction = "0";
                break;
            case "三乙":
                _Distinction = "1";
                break;
            case "二甲":
                _Distinction = "2";
                break;
            case "二乙":
                _Distinction = "3";
                break;
            case "一甲":
                _Distinction = "4";
                break;
            case "一乙":
                _Distinction = "5";
                break;
        }
        if (_Distinction != "全部")
        {
            strtemp = strtemp + " Distinction =" + _Distinction + " and";
        }
        if (_AreaId == "全部")
        {
            strtemp = strtemp + SysFun.AreaIdWhere1(Id_AreaId);
        }
        else
        {
            int _intAreaid = SysFun.ToInt(_AreaId), _intAreaIdMax = 0;
            string _strWhere = "";
            if (_intAreaid != 530000)
            {
                if (_intAreaid % 100 > 0)
                {
                    strtemp += "   Hospital.areaid=" + _intAreaid.ToString() + " and";
                }
                else
                {
                    _intAreaIdMax = _intAreaid + 100;
                    strtemp += " Hospital.areaid>=" + _intAreaid.ToString() + " And Hospital.AreaId< " + _intAreaIdMax.ToString() + " and";
                }
            }
            else
            {
                strtemp += " Hospital.areaid>530000 And Hospital.AreaId< 419900 and";
            }
        }
        if (_UserCode != "")
        {
            strtemp = strtemp + " UserCode like '%" + _UserCode + "%' and";
        }
        if (HospitalName != "")
        {
            strtemp = strtemp + " HospitalName like '%" + HospitalName + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }
    public string WriteHospital()
    {
        int i = 0;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Hospital";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = "";                                           //查询条件    
        dbPage.SelectField = "accountid,HospitalName,UserCode";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 24;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HospitalName,UserCode";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        sb.Append("<table  width='96%' border=0 align=center cellpadding=6 cellspacing=1>");
        while (dr.Read())
        {
            i++;
            sb.Append("<tr><td class='dotbottomline'>");
            sb.Append("<img src='../images/ico7.gif' width='8' height='5' hspace='4' vspace='1'>");
            sb.Append(dr["HospitalName"].ToString());

            sb.Append("</td></tr>");
        }
        for (int j = i; j < 24; j++)
            sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");

        sb.Append("</table>");
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;

        return sb.ToString();
    }
}
